package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.OrderDetails;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.OrderDetailsDto;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.OrderDetailsMapper;
import com.example.mall.query.OrderDetailsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.IOrderDetailsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单明细表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsMapper, OrderDetails> implements IOrderDetailsService {

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Override
    public PageDTO<OrderDetailsDto> queryPage(OrderDetailsQuery orderDetailsQuery) {
        if (orderDetailsQuery == null) {
            orderDetailsQuery = new OrderDetailsQuery();
        }
        Page<OrderDetails> page = orderDetailsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OrderDetails> cartPage = lambdaQuery()
                .like(orderDetailsQuery.getOrderId()!= null, OrderDetails::getOrderId, orderDetailsQuery.getOrderId())
                .like(orderDetailsQuery.getDetailId()!= null, OrderDetails::getDetailId, orderDetailsQuery.getDetailId())
                .like(orderDetailsQuery.getPrice()!= null, OrderDetails::getPrice, orderDetailsQuery.getPrice())
                .like(orderDetailsQuery.getProductId()!= null, OrderDetails::getProductId, orderDetailsQuery.getProductId())
                .like(orderDetailsQuery.getQuantity()!= null, OrderDetails::getQuantity, orderDetailsQuery.getQuantity())
                .like(orderDetailsQuery.getOrderNo()!= null, OrderDetails::getOrderNo, orderDetailsQuery.getOrderNo())
                .orderByDesc(OrderDetails::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, OrderDetailsDto.class);
    }

    @Override
    public OrderDetails queryOrderDetailsById(Long detailId) {
        AssertUtil.isNotNull(detailId, "订单明细ID不能为空");
        return orderDetailsMapper.selectById(detailId);
    }

    @Override
    public List<OrderDetails> queryOrderDetailsList() {
        return this.list();
    }

    @Override
    public void insert(OrderDetailsDto orderDetailsDto) {
        OrderDetails orderDetails = new OrderDetails();
        CopyUtils.copyProperties(orderDetailsDto, orderDetails);
        orderDetails.setDetailId(CodeGenerateUtils.generateUniqueId());
        orderDetails.setCreateDate(new Date());
        orderDetails.setUpdateDate(new Date());
        orderDetailsMapper.insert(orderDetails);
    }

    @Override
    public void delete(Long detailId) {
        AssertUtil.isNotNull(detailId, "订单明细ID不能为空");
        OrderDetails orderDetails = orderDetailsMapper.selectById(detailId);
        AssertUtil.isNotNull(orderDetails, "订单明细不存在");
        orderDetailsMapper.deleteById(detailId);
    }

    @Override
    public void deleteBatch(List<Long> detailIds) {
        AssertUtil.isNotNull(detailIds, "订单明细ID列表不能为空");
        List<OrderDetails> orderDetails = orderDetailsMapper.selectBatchIds(detailIds);
        AssertUtil.isNotNull(orderDetails, "订单明细不存在");
        orderDetailsMapper.deleteBatchIds(detailIds);
    }

    @Override
    public void update(OrderDetailsDto orderDetailsDto) {
        AssertUtil.isNotNull(orderDetailsDto.getDetailId(), "订单明细ID不能为空");
        OrderDetails orderDetails = orderDetailsMapper.selectById(orderDetailsDto.getDetailId());
        AssertUtil.isNotNull(orderDetails, "订单明细不存在");
        CopyUtils.copyProperties(orderDetailsDto, orderDetails);
        orderDetails.setUpdateDate(new Date());
        orderDetailsMapper.updateById(orderDetails);
    }
}
